﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DesignPatternToolkit.Wizard.Engine;
using EnvDTE;

namespace DesignPatternToolkit.Wizard.WizardPages
{
    public partial class ProjectSelector : WizardPageBase
    {
        public ProjectSelector()
        {
            InitializeComponent();
        }

        public override void BeforeLoad()
        {
            if (WizardConfiguration.Instance.VisualStudioEnv != null)
            {
                /* Find out the current projects */
                Solution currSolution = WizardConfiguration.Instance.VisualStudioEnv.Solution;

                if (currSolution.Projects.Count == 0)
                {
                    throw new ApplicationException("You haven't loaded a project yet. Please load a project first.");
                }

                foreach (Project project in currSolution.Projects)
                {
                    projectBox.Items.Add(project.Name);
                }

                if (!string.IsNullOrEmpty(WizardConfiguration.Instance.ProjectName))
                {
                    projectBox.SelectedValue = WizardConfiguration.Instance.ProjectName;
                }
                else
                {
                    if (currSolution.Projects.Count == 1)
                    {
                        projectBox.SelectedItem = projectBox.Items[0];
                    }
                }

            }
        }

        public override void BeforeUnload()
        {
            if (WizardConfiguration.Instance.VisualStudioEnv != null)
            {
                if (projectBox.SelectedItem == null)
                {
                    throw new ApplicationException("Please specify a project from the list.");
                }

                WizardConfiguration.Instance.ProjectName = projectBox.SelectedItem.ToString();
            }
        }
    }
}
